﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using Spark.Core.LoadBalancer;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Spark.Samples.WebApi.Controllers
{
    public class LongTest
    {
        public long Id { get; set; }

        public string Name { get; set; }
    }

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private ILogger<ValuesController> _logger;
        //private IConfigApi _client;

        public ValuesController(ILogger<ValuesController> logger
            )
        {
            _logger = logger;
            //var httpApiConfig = new HttpApiConfig
            //{
            //    HttpHost = new Uri("http://localhost"),
            //};
            //_client = HttpApiClient.Create<IConfigApi>(httpApiConfig);
        }

        [Route("~/wx")]
        [HttpGet]
        public async Task<IActionResult> WxLoginTest()
        {
            //var client = HttpApiClient.Create<IWxAppApi>();
            //var client = HttpApi.Resolve<IWxAppApi>();
            //AppToken token = null;
            //UserInfo user = null;
            //try
            //{
            //    //token = await client.GetToken("wxa7201a3315036ecb", "526475719219913d22b8efae26f328cb", "abcdefg");
            //    user = await client.GetUserInfo("adfadfadfadf", "adfadfasdfadfa");
            //}
            //catch (Exception e)
            //{
            //    Console.WriteLine(e);
            //    throw;
            //}

            //return Ok(user);

            return Ok(new LongTest { Id = 1120575645131812864, Name = "test" });
        }

        [HttpGet("{val}")]
        public IActionResult RedisSet(string val)
        {
            //RedisHelper.Set("abc", val);

            return Ok();
        }

        [HttpGet("{val}")]
        public IActionResult AddCache([FromServices]IDistributedCache cache, string val)
        {
            //cache.SetObject("abc", val);
            return Ok();
        }

        [HttpGet]
        public IActionResult GetCache([FromServices]IDistributedCache cache)
        {
            //object str = cache.GetObject("abc");
            return Ok();
        }

        [HttpGet("{key}")]
        public IActionResult RedisDel(string key)
        {
            //RedisHelper.Del(key);
            return Ok();
        }

        [HttpGet()]
        public IActionResult RedisSub()
        {
            //普通订阅
            //RedisHelper.Subscribe(
            //    ("chan1", msg =>
            //    {
            //        Console.WriteLine(msg.Body);
            //    }
            //));

            return Ok();
        }

        [HttpGet("{val}")]
        public IActionResult RedisPub(string val)
        {
            //RedisHelper.Publish("chan1", val);

            return Ok();
        }

        [HttpGet]
        public IActionResult RedisGet()
        {
            //var data = RedisHelper.HGetAll("abc");

            //string AbsoluteExpirationKey = "absexp";

            //string SlidingExpirationKey = "sldexp";
            //string DataKey = "data";

            //var ret = RedisHelper.HMGet<byte[]>("abc", AbsoluteExpirationKey, SlidingExpirationKey, DataKey);

            //var results = new object[]
            //{
            //    ret[0] == null ? null : Encoding.UTF8.GetString(ret[0]),
            //    ret[1] == null ? null : Encoding.UTF8.GetString(ret[1]),
            //    ret[2] == null ? null :  Encoding.UTF8.GetString(ret[2])
            //};

            //var temp = IDistributedCacheExtensions.Deserialize(ret[2]);

            //return Ok(temp);

            return Ok();
        }

        [HttpGet]
        public async Task<IActionResult> Test2([FromServices]ILoadBalancerHouse balancerHouse)
        {
            var balancer = await balancerHouse.Get("supplierservice");
            var result = await balancer.Lease();
            return Ok(result);
        }

        // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public ActionResult<string> Get(int id)
        {
            return "value";
        }

        // POST api/values
        [HttpPost]
        public IActionResult Post([FromBody] string value)
        {
            return Ok(1);
        }

        [HttpPost]
        public IActionResult Test([FromBody] string value)
        {
            _logger.LogInformation("abcdefg");
            throw new Exception("test");
            return Ok(1);
        }

        [HttpGet]
        public IActionResult TestLong(long id)
        {
            return Ok();
        }

        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}